import { ROLE_TYPE } from '@/utils/const/process';
export function createAuditStructure(process, createUser = null, proposer = null, authUser = null) {
  const structure = [];
  process.forEach(item => {
    const param = {
      ...item,
      examineRoles: [],
      id: item.id,
      isCountersign: item.isCountersign || 0,
      nodeType: item.nodeType,
      nodeName: item.nodeName
    };
    // roleType  0:任务创建人 1:选择部门 2:自定义人员 3:不限制审批人 4:申请人 5:主管
    item.examineRoles.forEach(role => {
      const roleParam = { aduitors: [], id: role.id, roleName: role.roleName, roleType: role.roleType, userRelation: role.userRelation };
      if ([ROLE_TYPE.CUSTOMIZE, ROLE_TYPE.DESIGNATE, ROLE_TYPE.CREATOR_SUPERIOR,
        ROLE_TYPE.DESIGNATE_DIRECTOR, ROLE_TYPE.DEPARTMENT,
        ROLE_TYPE.ROLE_GROUP, ROLE_TYPE.BP_APPROVER, ROLE_TYPE.FLYNOTICE,
        ROLE_TYPE.ARREARS_USERS].includes(role.roleType) && role.diyUserList) {
        if (role.isAutoFill) {
          roleParam.aduitors = role.diyUserList.map(jitem => {
            return {
              id: jitem.id,
              userName: jitem.userName,
              avatar: jitem.avatar,
              number: jitem.jobNmber
            };
          });
        } else if (role.isRandomFill && role.diyUserList.length) {
          let diyUserList = role.diyUserList;
          if (createUser) {
            diyUserList = diyUserList.filter(item => item.id !== createUser.id);
          }
          const len = diyUserList.length;
          if (len >= 1) {
            const randomIndex = Math.floor(Math.random() * len);
            roleParam.aduitors = [diyUserList[randomIndex] || diyUserList[0]];
          }
        }
      }

      if (role.roleType === ROLE_TYPE.PROPOSER && proposer) {
        const { id, userName, avatar, jobNumber } = proposer;
        roleParam.aduitors = [{ id, userName, avatar, number: jobNumber }];
      }
      // 选定主管
      if (role.diyUserList && role.roleType === ROLE_TYPE.DIRECTOR) {
        // 流程当前主管为一级主管则不回填，允许勾选其他人员
        if (role.roleType === ROLE_TYPE.DIRECTOR && role.isTopLeader) {
          roleParam.aduitors = [];
          roleParam.isTopLeader = true;
        } else {
          roleParam.aduitors = role.diyUserList.map(jitem => {
            return {
              id: jitem.id,
              userName: jitem.userName,
              avatar: jitem.avatar,
              number: jitem.jobNmber
            };
          });
        }
      }
      if (role.roleType === ROLE_TYPE.CREATOR && createUser) {
        const { id, userName, avatar, jobNumber } = createUser;
        roleParam.aduitors = [{ id, userName, avatar, number: jobNumber }];
      }
      if (role.roleType === ROLE_TYPE.INITIATING_NODE) { // 回填任务发起节点
        const arr = [];
        proposer && arr.push(proposer);
        createUser && arr.push(createUser);
        roleParam.aduitors = arr.map(jitem => {
          return {
            id: jitem.id,
            userName: jitem.userName,
            avatar: jitem.avatar,
            number: jitem.jobNmber
          };
        });
        roleParam.aduitors = uiniqueUser(roleParam.aduitors);
      }
      if (role.roleType === ROLE_TYPE.AUTHUSER && authUser) {
        const { id, userName, avatar, jobNumber } = authUser;
        roleParam.aduitors = [{ id, userName, avatar, number: jobNumber }];
      }
      param.examineRoles.push(roleParam);
    });
    structure.push(param);
  });
  return structure;
}
// 提交按钮审批列表回填用户，组件：/components/AuditProcess/auditDialog.vue
export function AutoFillUser(submitBtnList, createUser = null, proposer = null, authUser = null) {
  if (!Array.isArray(submitBtnList) || !submitBtnList.length) return;
  submitBtnList.forEach(role => {
    // 自定义选择人员
    if ([ROLE_TYPE.CUSTOMIZE, ROLE_TYPE.DESIGNATE, ROLE_TYPE.BP_APPROVER, ROLE_TYPE.DESIGNATE_DIRECTOR,
      ROLE_TYPE.CREATOR_SUPERIOR, ROLE_TYPE.ROLE_GROUP, ROLE_TYPE.ARREARS_USERS, ROLE_TYPE.DEPARTMENT
    ].includes(role.roleType) && role.diyUserList) {
      if (role.isAutoFill) {
        role._aduitors = role.diyUserList.map(jitem => {
          return {
            id: jitem.id,
            userName: jitem.userName,
            avatar: jitem.avatar,
            number: jitem.jobNmber
          };
        });
      } else if (role.isRandomFill && role.diyUserList.length) {
        let diyUserList = role.diyUserList;
        if (createUser) {
          diyUserList = diyUserList.filter(item => item.id !== createUser.id);
        }
        const len = diyUserList.length;
        if (len >= 1) {
          const randomIndex = Math.floor(Math.random() * len);
          role._aduitors = [diyUserList[randomIndex] || diyUserList[0]];
        }
      }
    }
    // 选定部门且自动填入 或主管
    if (role.diyUserList && role.roleType === ROLE_TYPE.DIRECTOR) {
      role._aduitors = role.diyUserList.map(jitem => {
        return {
          id: jitem.id,
          userName: jitem.userName,
          avatar: jitem.avatar,
          number: jitem.jobNmber
        };
      });
    }
    // 申请人
    if (role.roleType === ROLE_TYPE.PROPOSER && proposer) {
      const { id, userName, avatar, jobNumber } = proposer;
      role._aduitors = [{ id, userName, avatar, number: jobNumber }];
    }
    // 创建人
    if (role.roleType === ROLE_TYPE.CREATOR && createUser) {
      const { id, userName, avatar, jobNumber } = createUser;
      role._aduitors = [{ id, userName, avatar, number: jobNumber }];
    }
    // 授权人
    if (role.roleType === ROLE_TYPE.AUTHUSER && authUser) {
      const { id, userName, avatar, jobNumber } = authUser;
      role._aduitors = [{ id, userName, avatar, number: jobNumber }];
    }
  });
}

function uiniqueUser(arr) { // 根据id去除重复项
  const hash = {};
  var newArr = arr.reduce((item, next) => {
    if (!hash[next.id]) {
      hash[next.id] = true;
      item.push(next);
    }
    // hash[next.id] ? '' : hash[next.id] = true && item.push(next);
    return item;
  }, []);
  return newArr;
}
